(function(){
    /*
    * 所需工艺
    * */
    var gy={
        init:function(){
            this.wrap=$(".gy_wrap");
            this.btn=this.wrap.find(".gy_li");
            this.listWrap=$(".show_gy");
            this.list=this.listWrap.find("label");
            this.input=this.listWrap.find("input");
            this.str="";
            this.showBox();
        },
        showBox:function(){
            var _this=this;
            this.btn.on("click",function(event){
                event.stopPropagation();
                _this.listWrap.toggle();
            });

            $(document).on("click",function(){
                _this.listWrap.hide();
            });

            this.list.on("click",function(event){
                event.stopPropagation();
            });

            _this.input.on("click",function(event){
                event.stopPropagation();

                _this.str="";
                if($(this).is(":checked")){
                    $(this).parent().addClass("active");
                }else{
                    $(this).parent().removeClass("active");
                }

                _this.input.each(function(index){
                    if(_this.input.eq(index).is(":checked")){
                        _this.str+=_this.input.eq(index).parent().text()+" ";
                    }
                });
                _this.btn.val(_this.str);
            })

        }

    };

    /*
    * 日期
    * */
    var date={
        init:function() {
            this.datepicker($('#delivery_date'));
            this.datepicker($('#end_time'));
        },
        datepicker:function(obj){
            obj.datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                startDate: 'today'
            }).on("changeDate",function(event){
                $(this).removeClass("active").closest(".right").find(".error").remove();
            });
        }

    };

    var invite_add = {
    	init:function(){
            this.invite_insert();
        },
        invite_insert:function(){		/*新增询价*/
            var _this=this;
        	if($("#invite_insert").size() > 0){
        		$('form#invite_insert').submit(function(e) {

                    $("#invite_insert input.error").removeClass("error");

                    $("#r_area1 .select_area2 span").removeClass("error");
                    $("#r_area1 .select_area1 span").removeClass("error");

        			if($("form#invite_insert input[name=product_name]").val().length == 0){
                        
        				$("form#invite_insert input[name=product_name]").addClass("error");
            			layer.msg("产品名称 必填", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}
            		if($("input[name=main_technology_str]").val().length == 0){
            			$("input[name=main_technology_str]").addClass("error");
            			layer.msg("所需工艺 必选", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}
        			var num = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
            		if(!num.test($("input[name=one_num]").val())){ 
            			$("input[name=one_num]").addClass("error");
    			    	layer.msg("采购数量 请输入数字", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
    			    }
            		if($("input[name=delivery_time]").val().length == 0){
            			$("input[name=delivery_time]").addClass("error");
            			layer.msg("交货日期 必填", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}

                    if($("#r_area1 .select_area1 span").text()=="请选择"){
                        $("#r_area1 .select_area1 span").addClass("error");
                        layer.msg("收货地区 必填", {icon:2,time:1000});
                        _this.scrollTp();
                        return false;
                    }

                    if($("#r_area1 .select_area2 span").text()=="请选择"){
                        $("#r_area1 .select_area2 span").addClass("error");
                        layer.msg("收货地区 必填", {icon:2,time:1000});
                        _this.scrollTp();
                        return false;
                    }


            		if($("input[name=end_time]").val().length == 0){
            			$("input[name=end_time]").addClass("error");
            			layer.msg("截止日期 必填", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}
            		if($("input[name=link_man]").val().length == 0){
            			$("input[name=link_man]").addClass("error");
            			layer.msg("联系人 必填", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}
            		if(!/^1\d{10}$/.test($("input[name=link_phone]").val())){
            			layer.msg("联系方式 必填", {icon:2,time:1000});
            			$("input[name=link_phone]").val('').addClass("error");
                        _this.scrollTp();
    			        return false;
            		}

            		var cu=confirm('发布后不能修改，确认发布？');
            		if(cu==false){
            			return false;
            		}
            		
        			$.ajax({
    			        type:"POST",
    			        dataType:"json",
    			        async: false,
                        url: get_baseUrl()+"Cinvite/addPost",
                        data: $("form#invite_insert").serialize(),
                        beforeSend: function() {
                            $("#invite_insert input[type=submit]").val("正在提交...").attr("disabled", true);
                        },
    			        success: function(json) {
                            if (json.status) {
                            	layer.msg(json.errmsg, {icon:1,time:1000}, function(){
                            		window.location.href = json.data;
                            	});
                            } else {
                                $("#invite_insert input[type=submit]").val("确认发布").removeAttr("disabled");
                                layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                                	if(json.data){window.location.href = json.data;}
                                });
                            }
                        }
    			    });
        			
                    e.preventDefault();
                });
        	}
        },

        scrollTp:function(){
            $(".add_ul").find(".error").each(function(index){
                $("html,body").stop().animate({scrollTop:$(this).offset().top-30},600,"easeInOutQuint");
                return false;
            });
        }
    };

    /*
    * 上传图片
    * */
    var upLoadImage={
        init:function(){
            this.str="";
            this.btn=$("#file-up1");
            this.content=$(".xj-add-pic ul");
            this.addPic();
            this.deletePic();
        },
        addPic:function(){
            var _this=this;
            this.btn.on("change",function(){
                var file = this.files[0];
                if (file.size > 5120 * 1024) {
                    layer.msg('图片不能超过5M', {icon: 2, time: 1000});
                    return;
                } else if (_this.content.children().length + 1 > 10) {
                	layer.msg("图片只能上传10张", {icon: 2, time: 1000});
                    return;
                }
                var src = window.URL.createObjectURL(file);
                $.ajaxFileUpload({
                    url: get_baseUrl()+'Cinvite/file_add', //你处理上传文件的服务端
                    secureuri: false,
                    fileElementId:"file-up1",
                    data: {},
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            str = '<li><img src="'+src+'" alt=""><span class="xj-delete2" data-fileid="'+data+'"></span></li>';
                            _this.content.prepend(str);
                            if(_this.content.children().length>0){
                                $(".add-send-btn span").text("继续上传");
                            }
                        } else {
                            layer.msg('上传失败', {icon: 2, time: 1000});
                        }
                    }
                })
            });
        },
        deletePic:function(){
            $(".xj-add-pic").on("click",".xj-delete2",function(){
                var this_id = $(this);
                $.post(get_baseUrl() + 'Cinvite/file_delete', {file: this_id.data('fileid')}, function (json) {
                    if(json) {
                        this_id.parent('li').remove();
                        if(this_id.parents('ul').children().length==0){
                            $(".add-send-btn span").text("上传附件");
                        }
                    }
                }, 'json');

            });
        }
    };

    /*
    *地区
    * */
    var area={
        init:function(){
            this.area2();
        },
        area2:function(){  /*地区选择*/
            var arr=[$("#r_area1 .select_area1"),$("#r_area1 .select_area2")];
            var arr2=[$("#r_area2 .select_area1"),$("#r_area2 .select_area2")];
            this.areaShow(arr[0],arr[1]);
            this.areaShow(arr2[0],arr2[1]);
        },
        areaShow:function(obj1,obj2){
            this.showBox(obj1);
            this.showBox(obj2);
            var $em=obj1.find("em");
            $em.on("click",function(event){
                event.stopPropagation();
                $(this).closest("div").hide();
                obj1.find("span").html($(this).text())    ;
                obj1.find("input").val($(this).text());
                obj2.find("p").hide().eq($(this).index()).show();
                obj2.find("span").html("请选择");
                obj2.find("input").val("请选择");
            });

            var $em2=obj2.find("em");

            $em2.on("click",function(event){
                event.stopPropagation();
                obj2.find("span").html($(this).text());
                obj2.find("input").val($(this).text());
                $(this).closest("div").hide();
            });

            $(document).on("click",function(){
                $(".select_list").hide();
            });
        },
        showBox:function(obj){
            obj.on("click","span",function(event){
                event.stopPropagation();
                $(this).parent().find(".select_list").toggle().end().siblings().find(".select_list").hide();
            });
        }
    };

    gy.init();
    date.init();
    invite_add.init();
    area.init();
    upLoadImage.init();

})();